using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Threading;
using FreeLancer.HRB.Core.Web.Controls;
using FreeLancer.HRB.Domains;
using FreeLancer.HRB.Core;
using DevExpress.Web.ASPxClasses;

namespace FreeLancer.HRB.WebApp
{
  public partial class Default : WebBasePage
  {
    private const string NeedingsCategoryId = "792ad48a-3bec-4565-8600-5f9162e4b474";
    private const string ActivitiesCategoryId = "738a6876-1003-435e-8061-1f13adc8f6e5";
    private const string NationalNewsId = "3c2a2d2a-52ad-4b3c-a635-f3bd2900e443";
    private const string InternationalNewsId = "0e573537-7ab7-46c1-bcb7-90663a50abd7";
    private const string CategoryPage = "~/Category.aspx?id=";

    protected void Page_Init(object sender, EventArgs e)
    {
      InitializeNewsBrowser();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
      InitializeDataSource();
      InitializeImages();
    }

    private void InitializeNewsBrowser()
    {
      NewsBrowserNeedings.MoreNewsHref = CategoryPage + NeedingsCategoryId;
      NewsBrowserActivities.MoreNewsHref = CategoryPage + ActivitiesCategoryId;
      NewsBrowserNationalNews.MoreNewsHref = CategoryPage + NationalNewsId;
      NewsBrowserInternationNews.MoreNewsHref = CategoryPage + InternationalNewsId;
    }

    private void InitializeDataSource()
    {
      DataSourceNeedings.SelectParameters.Clear();
      DataSourceNeedings.SelectParameters.Add("LanguageId", _languageId.ToString());
      DataSourceNeedings.SelectParameters.Add("CategoryId", NeedingsCategoryId);
      DataSourceNeedings.SelectParameters.Add("IsPublished", "True");
      DataSourceNeedings.SelectParameters.Add("HeaderImageFolder", 
        Utils.RelativeWebRoot + "Images/PostImages/");

      DataSourceActivities.SelectParameters.Clear();
      DataSourceActivities.SelectParameters.Add("LanguageId", _languageId.ToString());
      DataSourceActivities.SelectParameters.Add("CategoryId", ActivitiesCategoryId);
      DataSourceActivities.SelectParameters.Add("IsPublished", "True");
      DataSourceActivities.SelectParameters.Add("HeaderImageFolder", 
        Utils.RelativeWebRoot + "Images/PostImages/");

      DataSourceNationalNews.SelectParameters.Clear();
      DataSourceNationalNews.SelectParameters.Add("LanguageId", _languageId.ToString());
      DataSourceNationalNews.SelectParameters.Add("CategoryId", NationalNewsId);
      DataSourceNationalNews.SelectParameters.Add("IsPublished", "True");
      DataSourceNationalNews.SelectParameters.Add("HeaderImageFolder", 
        Utils.RelativeWebRoot + "Images/PostImages/");

      DataSourceInternationNews.SelectParameters.Clear();
      DataSourceInternationNews.SelectParameters.Add("LanguageId", _languageId.ToString());
      DataSourceInternationNews.SelectParameters.Add("CategoryId", InternationalNewsId);
      DataSourceInternationNews.SelectParameters.Add("IsPublished", "True");
      DataSourceInternationNews.SelectParameters.Add("HeaderImageFolder", 
        Utils.RelativeWebRoot + "Images/PostImages/");
    }

    private void InitializeImages()
    {
      int total = 0;
      TList<FreeLancer.HRB.Domains.Events> events = //GlobalCache.EventsService.GetByLanguageId(_languageId);
        GlobalCache.EventsService.GetPaged("[IsPublished]='True' AND [LanguageID]=" + _languageId, "[DateCreated] DESC", 0, 1, out total);

      TList<FreeLancer.HRB.Domains.Images> images = new TList<Images>();
      if (events.Count > 0)
        images = GlobalCache.ImagesService.GetByEventIdLanguageId(events[0].EventId, _languageId);

      ASPxDataView.DataSource = images;
      ASPxDataView.DataBind();
    }

    protected void ASPxDataView_OnCustomCallback(object sender, CallbackEventArgsBase e)
    {
      int pageIndex = ASPxDataView.PageIndex;
      pageIndex++;
      if (pageIndex == ASPxDataView.PageCount)
        pageIndex = 0;

      ASPxDataView.PageIndex = pageIndex;
    }
  }
}
